package leetcodecompetition.S110;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Siantar
 * @Date: 2023-08-05-23:17
 * @Description: 1.0
 */
public class T6956 {
    public int minimumSeconds(List<Integer> nums) {
        int len = nums.size();
        nums.addAll(nums);
        Map<Integer, int[]> map = new HashMap<>();
        for (int i = 0; i < len * 2; i++) {
            int num = nums.get(i);
            if (map.containsKey(num)) {
                int[] v = map.get(num);
                int index = v[0], maxLen = v[1];
                if (i - index > maxLen) {
                    maxLen = i - index;
                }
                map.put(num, new int[]{i, maxLen});
            } else {
                map.put(num, new int[]{i, 0});
            }
        }
        int res = len;
        for (Map.Entry<Integer, int[]> e : map.entrySet()) {
            if (e.getValue()[1] < res) {
                res = e.getValue()[1];
            }
        }
        return (res - 1) / 2 + (res - 1) % 2;
    }

    @Test
    public void test() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(1);
        list.add(5);
        list.addAll(list);
        System.out.println(list);
    }
}
